package com.dmall.sharding.jdbc.controller;

import com.dmall.sharding.jdbc.domain.User;
import com.dmall.sharding.jdbc.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/user/detail/{id}")
    public User detail(@PathVariable("id") Integer id) {
        return userService.findById(id);
    }

    @RequestMapping("/user/save")
    public String save(User user) {
        user.setBirth(LocalDate.now());
        userService.save(user);
        return "OK";
    }

    @RequestMapping("/user/select")
    public Object select(String sql) {
        Object select = userService.select(sql);
        return select;
    }

    @RequestMapping("/user/insert")
    public Object insert(String sql) {
        Object object = userService.insert(sql);
        return object;
    }
}
